<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\rehearse\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 预案管理
 * @package app\plan
 * @author LJQ<245369209@qq.com>
 */
class Plan extends Api

{

    /**
   *  脚本列表
   * @param string 
   * @return Json
   */

  public function planList($planId=null){
              
              $where= [] ;
              $with='scripts,unit';
              $where['formId']=2;
              $where['natureId']=2;
               if ($planId) {
                      $where['id']=['eq',$planId]; 
                  }
              
    //分页参数
    if(($limit = input('limit')) && ($page = input('page'))){
      $startLimit =  ($page-1)*$limit;
      // 脚本列表
      $info =collection(model('plan')->with($with)->where($where)->limit($startLimit.','.$limit)->select())->toArray();
    }else{
      //脚本列表
      $info =collection(model('plan')->with($with)->where($where)->select())->toArray();
    }
       
    //总数
       $count = model('plan')->where($where)->count('id'); 
    
              if ($info && $count == 0) {
              return json(['code'=>1,'msg'=>'没有找到脚本','count'=>$count,'data'=>$info]);
          } 
        
          if ($count > 0){
            return json(['code'=>1,'msg'=>'脚本','count'=>$count,'data'=>$info]);
          }
                return json(['code'=>0,'msg'=>'数据异常']);
       }
      
          //添加脚本
     public function add($data){
          
          if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }
          $model = model('plan/plan');
          $result = $model->change($data);
          if (false != $result) {
             setlog('planadd','plan',session('user_auth.id'));
              return json(['code'=>1,'msg'=>'脚本添加成功']);
          } else {
              return json(['code'=>0,'msg'=>'脚本添加失败,请检查']);
          }
      }

    

   /**
   * 修改脚本
   * @param  string planId 脚本id,
   * @return Json
   */
    public function edit($data,$planId){
        
          if (empty($planId)) {
              return json(['code'=>0,'msg'=>'      脚本不存在,请检查']);
          }
          $model = model('plan/plan');     
          if ($model->change($data)) {
            setlog('planupdate','plan',session('user_auth.id'));
             return json(['code'=>1,'msg'=>'脚本修改成功']);
            }else{
             return json(['code'=>0,'msg'=>'脚本修改失败,请检查']);
            }
    }
    /**
   *  删除脚本
   * @param string planId 脚本id,
   * @param Json
   */
     
    public function del($planId) {
        $planId = input('planId',0,'intval');
        $model = model('plan/plan');
        if (empty ( $planId )||empty(($model->where(array('id'=>$planId))->find()))) {
            return  json(['code'=>0,'msg'=>'脚本id不存在,请检查']);
        }
        if($model->destroy(['id'=>$planId]) !== FALSE){
          setlog('plandel','plan',session('user_auth.id'));
           return json(['code'=>1,'msg'=>'脚本删除成功']);
       }else{
           return $this->json(['code'=>0,'msg'=>'脚本删除失败,请检查']);
       }
    }

     
   /**
   *   查询脚本
   * @param keyword脚本名称
   * @return Json
   */
    public function search($typeId=null,$unitId=null){
          
          $where= [] ;
          $with='scripts,unit';
          $where['natureId']=2;
          $where['formId']=2;
            if($typeId){
                  $where['typeId'] = ['eq',$typeId];
                }

            if($unitId){
                         $where['unitId'] = ['eq',$unitId];
                }
           

      //分页参数
    if(($limit = input('limit')) && ($page = input('page'))){
      $startLimit =  ($page-1)*$limit;
      //脚本列表
      $info =collection(model('plan')->with($with)->where($where)->limit($startLimit.','.$limit)->select())->toArray();
    }else{
      //脚本列表
      $info =collection(model('plan')->with($with)->where($where)->select())->toArray();
    }
    //总数
         $count = model('plan')->where($where)->count('id'); 
        
     
             if (empty($info) && $count == 0) {
                return json(['code'=>0,'msg'=>'没有找到脚本','count'=>$count]);
        } 
        
        if ($count > 0){
          return json(['code'=>1,'msg'=>'脚本','count'=>$count,'data'=>$info]);
        }
      } 

      
      /**
       * 申请审核
       * @param string $hazardId
       */
      public function audit($planId = null){
      	$wfType = 1;
      	$workflow = model('Workflow')->where('wfType',$wfType)->find();
      	if(empty($workflow)||empty($workflow->steps)){
      		return json(['code'=>0,'msg'=>'无对应审核流程','count'=>'','data'=>'']);
      	}else{
      		$plan = model('Plan')->get($planId);
      		if($plan->isUpdate(true)->save(['checkStatus'=>2]) !== false){
      			$data['title'] = $plan['title'];
      			$data['wfFlowInfoId'] = $workflow['id'];
      			$data['wfType'] = $wfType;
      			$data['busiId'] = $planId;
      			$data['applyPerson'] = session('user_auth.id');
      			if(model('Wfitem')->save($data)){
      				return json(['code'=>1,'msg'=>'申请成功','count'=>'','data'=>'']);
      			}else{
      				return json(['code'=>0,'msg'=>'申请失败','count'=>'','data'=>'']);
      			}
      		}
      	}
      	return json(['code'=>0,'msg'=>'申请失败','count'=>'','data'=>'']);
      
      }

      
}